$(function () {
    setStatus();
    getTeamList();
    setLeader();
});
//为当前显示条数文本框绑定回车事件
$('#size').bind('keypress', function (event) {
    if (event.keyCode == "13") {
        //敲击回车后重新按照新的显示条数加载页面
        getTeamList();
    }
});

//为当前页数文本框绑定回车事件
$('#pageipt').bind('keypress', function (event) {
    if (event.keyCode == "13") {
        getTeamList();
    }
});
var total = 0;
var index = 1;
var pageSize = 5;
var leaderName = new Array();
var leaderCode = new Array();
//为上一页绑定点击事件
$('#lastpage').click(function () {
    //获取默认的当前页数和显示条数值
    let page = $("#pageipt").val();
    page--;
    if (page >= index) {//如果不是第一页，则往前翻一页
        $("#pageipt").val(page);
        getTeamList();
    }
});

//绑定下一页点击事件
$('#nextpage').click(function () {
    //获取默认的当前页数和显示条数值
    let page = $("#pageipt").val();
    let size = $("#size").val();
    page++;
    if (page <= Math.ceil(total / size)) {
        $("#pageipt").val(page);
        getTeamList();
    }
});

//为首页绑定点击事件
$("#first").click(function () {
    let page = $("#pageipt").val();
    if (page > index) {//如果当前页不是首页则跳转到首页，否则不做任何事情
        page = index;
        $("#pageipt").val(page);
        getTeamList();
    }
});

//为尾页绑定点击事件
$("#last").click(function () {
    let page = $("#pageipt").val();
    let size = $("#size").val();
    if (page < Math.ceil(total / size)) {//如果当前页不是尾页，则跳转到尾页
        page = Math.ceil(total / size);
        $("#pageipt").val(page);
        getTeamList();
    }
});

function getTeamList() {
    let param = $("#search_ipt").val();
    let start_date = $("#start_date").val();
    let end_date = $("#end_date").val();
    let leader = $("#leader_select").find("option:selected").val();
    let status = $("#status_select").find("option:selected").val();
    let sort = $("#sort").find("option:selected").val();
    let page = $("#pageipt").val();
    let size = $("#size").val();
    if (page.trim() == '' || parseInt(page.trim()) < index)
        page = index;
    if (size.trim() == '' || parseInt(size.trim()) < index)
        size = pageSize;
    let map = {
        "param": param,
        "start_date": start_date,
        "end_date": end_date,
        "leader": leader,
        "status": status,
        "sort": sort,
        "page": page,
        "size": size
    };
    let data = myAjax("/team/getTeamList", "get", map, "json");
    console.log(data);
    $("tbody").html("");
    $.each(data.list, function (i, team) {
        if (team.leader!=null) {
            leaderCode.push(team.leader.username);
            leaderName.push(team.leader.name);
        }
        //给小组列表赋值
        $("tbody").append("<tr><td><input type='checkbox'></td><td>" + team.id + "</td><td>" + team.code + "</td>" +
            "<td>" + team.name + "</td><td>" + (team.leader == null ? "暂无组长" : team.leader.name) + "</td><td>" + (team.leader == null ? "noleader" : team.leader.username) + "</td>" +
            "<td>" + team.crew + "</td><td>" + team.status.name + "</td><td>" + team.createTime + "</td>" +
            "<td>" + (team.updateTime == null ? "暂无更新" : team.updateTime) + "</td><td><button class='iconfont' onclick='editTeam(" + JSON.stringify(team.code) + ")'>&#xe608;</button><button class='iconfont' onclick='deleteTeam(" + team.id + ")'>&#xe618;</button></td></tr>");
    });
    total = data.total;
    //显示总条数
    $("#pagecount").text(total);
    console.log(total);
}

function editTeam(code) {
    sessionStorage.setItem("team_code", code);
    window.location.href = "../html/team/editteam.html";
}

function deleteTeam(id) {
    let flag = confirm("确定删除该小组吗？");
    if (flag) {
        let data = myAjax("/team/deleteTeam", "post", {"id": id}, "json");
        if (data > 0)
            alert("删除成功！");
    }
}

function addTeam() {
    let code = $("#add_code").val();
    let name = $("#add_name").val();
    if (checkCode()) {
        if (code != "" && name != "") {
            let data = {
                "name": name,
                "code": code
            };
            let result = myAjax("/team/addTeam", "post", data, "json");
            if (result == 200)
                alert("小组创建成功");
            else if (result == 500)
                alert("创建失败");
            else
                alert("找不到方法");
            $(".right").load("../html/team/team.html");
        } else
            alert("小组名称和编号不能为空");
    }
}

function checkCode() {
    let code = $("#add_code").val();
    let data = myAjax("/team/getTeamByCode", "get", {"code": code}, "json");
    console.log(data);
    if (data.code == 200) {
        alert("该小组编号已被使用，请重新输入");
        return false;
    } else
        return true;
}
function setStatus() {
    let status = myAjax("/data/getStatusList","get",{},"json");
    $("#status_select").html("<option value='全部'>全部</option>");
    $.each(status, function (i, option) {
        $("#status_select").append("<option value=" + option.code + ">" + option.name + "</option>");
    });
}
function setLeader() {
    const code = leaderCode;
    const name = leaderName;
    $("#leader_select").html("<option value='全部'>全部</option>");
    for (let i=0;i<leaderName.length;i++) {
        //给组长下拉框赋值
        $("#leader_select").append("<option value='"+code[i]+"'>"+name[i]+"</option>");

    }
}